package com.gcsoftware.web.service;

import com.gcsoftware.Interceptor.LoginCache;
import com.gcsoftware.beans.PageObj;
import com.gcsoftware.beans.SysLog;
import com.gcsoftware.beans.SysUser;
import com.gcsoftware.web.mapper.db.LoginMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.StringTokenizer;

import static com.gcsoftware.Interceptor.LoginCache.USER_NAME;
import static com.gcsoftware.configs.AppConstants.Sys.*;

/**
 * 登陆
 * @author czp
 * date 2018-06-20
 */
@Service
public class LoginService {
    @Autowired
    private LoginMapper loginMapper;

    public String loginStatus(String name, String pwd, HttpServletRequest request) {
        SysUser sysUser = loginMapper.querySysUser();
        String sessionId = request.getSession().getId();
        String cacheSessionId = LoginCache.LOGIN_SESSION_ID.get(SYS_SESSION_ID.getValue());
        if (!name.equals(sysUser.getC_user_name()) || !pwd.equals(sysUser.getC_user_pwd())) {
            return "FAILED";
        } else if (cacheSessionId == null){
            recordLog(request, SYS_LOGIN.getName(), name, sessionId);
            init(name);
            return "SUCCESSFUL";
        } else if (sessionId.equals(cacheSessionId)){
            init(name);
            return "SUCCESSFUL";
        } else {
            return "INTERCEPTOR";
        }
    }

    private void init(String userName) {
        LoginCache.USER_NAME = userName;
    }

    private void recordLog(HttpServletRequest request, String state, String name, String sessionId) {
        String Agent = request.getHeader("User-Agent");
        StringTokenizer st = new StringTokenizer(Agent, ";");
        String ip = request.getRemoteAddr();
        String browser = st.nextToken();
        LoginCache.LOGIN_SESSION_ID.put(SYS_SESSION_ID.getValue(), sessionId);
        LoginCache.LOGIN_IP.put(SYS_LOGIN_IP.getValue(), ip);
        LoginCache.LOGIN_BROWSER.put(SYS_LOGIN_BROWSER.getValue(), browser);
        loginMapper.recordLog(new SysLog( name, ip, browser, state ));
    }

    public boolean loginOut(HttpServletRequest request, String state) {
        String Agent = request.getHeader("User-Agent");
        StringTokenizer st = new StringTokenizer(Agent, ";");
        request.getSession().removeAttribute(LoginCache.LOGIN_SESSION_ID.get(SYS_SESSION_ID.getValue()));
        LoginCache.LOGIN_SESSION_ID.remove(SYS_SESSION_ID.getValue());
        LoginCache.LOGIN_IP.remove(SYS_LOGIN_IP.getValue());
        LoginCache.LOGIN_BROWSER.remove(SYS_LOGIN_BROWSER.getValue());
        loginMapper.recordLog(new SysLog(
                USER_NAME,
                request.getRemoteAddr(),
                st.nextToken(),
                state
        ));
        return true;
    }

    public PageObj<SysLog> getSysLogs(String loginAction, String time, int page, int rows, String sord) {
        PageHelper.startPage(page, rows);
        List<SysLog> logsList = loginMapper.getSysLogs();
        PageInfo<SysLog> pageData = new PageInfo<SysLog>(logsList);
        return new PageObj<SysLog>(
                page,
                pageData.getPages(),
                (int)pageData.getTotal(),
                logsList
        );
    }
}
